package com.buobao.jdbc.demo;

import java.sql.*;

public class DbUtil {
    /**
     * 创建新的数据库连接
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai","root",null);
    }

    /**
     * 关闭连接
     * @param rs
     * @param statement
     * @param connection
     * @throws SQLException
     */
    public static  void closeConnection(ResultSet rs, Statement statement, Connection connection) throws SQLException {
        if (rs!=null) {
            rs.close();
        }

        if (statement!=null && statement.isClosed()==false) {
            statement.close();
        }

        if (connection!=null && connection.isClosed()==false) {
            connection.close();
        }
    }
}
